---
title: "Hello Minikube"
categories:
- devops
tags:
---

<div id="content">
<div id="table-of-contents">
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#orgbda6cbd">Preparation</a></li>
<li><a href="#org0832a85">Create a deployment</a></li>
<li><a href="#orgf103a15">Create a Service</a></li>
<li><a href="#org27fa105">Clean Up</a></li>
</ul>
</div>
</div>
<blockquote>
<p>
<a href="https://kubernetes.io/docs/tutorials/hello-minikube/">https://kubernetes.io/docs/tutorials/hello-minikube/</a>
</p>
</blockquote>
<div class="outline-2" id="outline-container-orgbda6cbd">
<h2 id="orgbda6cbd">Preparation</h2>
<div class="outline-text-2" id="text-orgbda6cbd">
<p>
下面的两个文件是 hello-node 的源文件
</p>
<pre class="example" id="org6b9c540">
var http = require('http');

var handleRequest = function(request, response) {
  console.log('Received request for URL: ' + request.url);
  response.writeHead(200);
  response.end('Hello World!');
};
var www = http.createServer(handleRequest);
www.listen(8080);
</pre>
<pre class="example" id="org1b90a86">
FROM node:6.14.2
EXPOSE 8080
COPY server.js .
CMD node server.js
</pre>
</div>
</div>
<div class="outline-2" id="outline-container-org0832a85">
<h2 id="org0832a85">Create a deployment</h2>
<div class="outline-text-2" id="text-org0832a85">
<p>
使用 {% raw %} kubectl create {% endraw %} 命令创建一个包含一个pod的Deployment，那个pod使用 {% raw %} gcr.io/hello-minikube-zero-install/hello-node {% endraw %} 这个镜像运行了一个容器。
</p>
<div class="org-src-container">
<pre class="src src-sh">kubectl create deployment hello-node --image=gcr.io/hello-minikube-zero-install/hello-node
</pre>
</div>
<p>
查看创建的Deployment：
</p>
<div class="org-src-container">
<pre class="src src-sh">kubectl get deployments
<span style="font-weight: bold; font-style: italic;">## </span><span style="font-weight: bold; font-style: italic;">OUTPUT</span>
NAME         DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
hello-node   1         1         1            1           1m
</pre>
</div>
<p>
查看pod：
</p>
<div class="org-src-container">
<pre class="src src-sh">kubectl get pods
<span style="font-weight: bold; font-style: italic;">## </span><span style="font-weight: bold; font-style: italic;">OUTPUT</span>
NAME                          READY     STATUS    RESTARTS   AGE
hello-node-5f76cf6ccf-br9b5   1/1       Running   0          1m
</pre>
</div>
<p>
查看事件
</p>
<div class="org-src-container">
<pre class="src src-sh">kubectl get events
</pre>
</div>
<p>
查看配置
</p>
<div class="org-src-container">
<pre class="src src-sh">kubectl config view
</pre>
</div>
</div>
</div>
<div class="outline-2" id="outline-container-orgf103a15">
<h2 id="orgf103a15">Create a Service</h2>
<div class="outline-text-2" id="text-orgf103a15">
<p>
默认情况下，pod只能在kubernates容器内部通过内部IP地址访问，为了让 hello-node 容器能从kubernates虚拟网络外部访问，你需要将pod包装成一个Kubernates服务：
</p>
<div class="org-src-container">
<pre class="src src-sh">kubectl expose deployment hello-node --type=LoadBalancer --port=8080
</pre>
</div>
<p>
–type=LoadBalancer标志表示希望将服务暴露到集群外面
</p>
<p>
查看刚刚创建的服务：
</p>
<div class="org-src-container">
<pre class="src src-sh">kubectl get services
<span style="font-weight: bold; font-style: italic;">## </span><span style="font-weight: bold; font-style: italic;">OUTPUT</span>
NAME         TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
hello-node   LoadBalancer   10.108.144.78   &lt;pending&gt;     8080:30369/TCP   21s
kubernetes   ClusterIP      10.96.0.1       &lt;none&gt;        443/TCP          23m
</pre>
</div>
<p>
运行服务：
</p>
<div class="org-src-container">
<pre class="src src-sh">minikube service hello-node
</pre>
</div>
</div>
</div>
<div class="outline-2" id="outline-container-org27fa105">
<h2 id="org27fa105">Clean Up</h2>
<div class="outline-text-2" id="text-org27fa105">
<div class="org-src-container">
<pre class="src src-sh">kubectl delete service hello-node
kubectl delete deployment hello-node
</pre>
</div>
</div>
</div>
</div>
<div class="status" id="postamble">
<p class="author">Author: amo</p>
<p class="validation"><a href="http://validator.w3.org/check?uri=referer">Validate</a></p>
</div>
